package com.wj.facedetect.net;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.wj.facedetect.bean.common.CodeEnum;
import com.wj.facedetect.bean.common.NullValueHandler;

public class BaseResponse<T> {
    private static final String TAG = "BaseResponse";
    private boolean success;
    private Class<T> cls;
    /***服务器字段{code:xxx,message:xxx,result:xxx}***/
    private int code;
    private String message;
    private T results;

    public BaseResponse(Class<T> cls) {
        this.cls = cls;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public boolean isSuccess() {
        return success;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getResults() {
        return results;
    }

    public void setError(CodeEnum codeEnum) {
        this.code = codeEnum.getCode();
        this.message = codeEnum.getMessage();
    }

    protected void parseIt(String json) {
        if (TextUtils.isEmpty(json)) {
            success = false;
            setError(CodeEnum.dataEmpty);
            return;
        } else {
            success = true;
            Gson gson = new Gson();
            results = gson.fromJson(json, cls);
            if (results == null) {
            } else if (results instanceof NullValueHandler) {
                ((NullValueHandler) results).handNullValue();
            }
        }


    }
}

